<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <h2>电商后台管理系统</h2>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
<!--      侧边栏-->
      <el-aside :width="collWidth">
<!--        折叠-->
        <div class="taggle-button" @click="toggleCollapse">|||</div>
<!--        collapse折叠-->
        <el-menu
          :default-active="activePath"
          router
          :collapse = "isToggleCollapse"
          :collapse-transition="false"
          unique-opened
          background-color="#34495e"
          text-color="#fff"
          active-text-color="#ffd04b">
          <el-submenu :index="item.id | nullSting" v-for="item in AsideListDate" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item @click="setActivePath('/home/'+sonItem.path)" :index="sonItem.path|addHomePath" v-for="sonItem in item.children" :key="sonItem.id" >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ sonItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <keep-alive>
          <router-view></router-view>
        </keep-alive>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'Home',
  data () {
    return {
      // 是否折叠菜单
      isToggleCollapse: false,
      // 左侧菜单列表数据
      AsideListDate: [],
      // 图标
      iconObj: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-logooo_huabanfuben',
        145: 'iconfont icon-shuju'
      }
    }
  },
  computed: {
    ...mapState(['activePath']),
    collWidth () {
      return this.isToggleCollapse ? '64px' : '200px'
    }
  },
  created () {
    this.getAsideList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.info('退出成功，请重新登录')
    },
    // 获取左侧菜单数据
    async getAsideList () {
      const { data } = await this.$axios.get('menus')
      if (data.meta.status === 200) {
        this.AsideListDate = data.data
      } else {
        this.$message.error(data.meta.msg)
      }
    },
    // 点击按钮折叠菜单
    toggleCollapse () {
      this.isToggleCollapse = !this.isToggleCollapse
    },
    // 点击二级菜单保存路径到Vuex
    setActivePath (payload) {
      this.$store.commit({
        type: 'updateActivePath',
        path: payload
      })
    }
  }
}
</script>

<style lang="less" scoped>
.home_container{
  height: 100%;
}
.el-header{
  //padding-left: 0px;
  background-color: #2c3e50;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ecf0f1;
}
.el-aside{
  background-color: #34495e;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #ecf0f1;
}
.iconfont{
  margin-right: 10px;
}
//折叠样式
.taggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor:pointer;
}
</style>
